/**
 * 
 */
package belote.webapp.struts.user;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.GenericValidator;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import belote.service.user.UserService;
import belote.service.user.UserServiceFactory;

/**
 * @author Gwenn
 * 
 */
@SuppressWarnings("serial")
public class RegistrationForm extends ActionForm {

	// ========================================================================
	// CONSTANTES
	// ========================================================================

	private static final String REGEX_FORMAT_LOGIN = "^[a-zA-Z0-9\u00C0-\u00FF]+$";

	private static final int LOGIN_MIN_LENGTH = 3;

	private static final int LOGIN_MAX_LENGTH = 16;

	private static final int PASSWORD_MIN_LENGTH = 7;

	private static final int PASSWORD_MAX_LENGTH = 16;

	// ========================================================================
	// ATTRIBUTS
	// ========================================================================

	private String login;

	private String password;

	private String email;

	private String genre;

	// ========================================================================
	// ATTRIBUTS
	// ========================================================================

	private final UserService userService = UserServiceFactory.getUserService();

	// ========================================================================
	// METHODES 'PUBLIC'
	// ========================================================================

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ActionErrors validate(ActionMapping pMapping, HttpServletRequest pRequest) {

		ActionErrors errors = new ActionErrors();

		// --- login
		if (GenericValidator.isBlankOrNull(login)) {
			errors.add("login", new ActionMessage("inscription.form.login.required"));
		} else if (!GenericValidator.minLength(login, LOGIN_MIN_LENGTH)) {
			errors.add("login", new ActionMessage("inscription.form.login.minLength", LOGIN_MIN_LENGTH));
		} else if (!GenericValidator.maxLength(login, LOGIN_MAX_LENGTH)) {
			errors.add("login", new ActionMessage("inscription.form.login.maxLength", LOGIN_MAX_LENGTH));
		} else if (!GenericValidator.matchRegexp(login, REGEX_FORMAT_LOGIN)) {
			errors.add("login", new ActionMessage("inscription.form.login.format", LOGIN_MIN_LENGTH, LOGIN_MAX_LENGTH));
		} else if (loginExists()) {
			errors.add("login", new ActionMessage("inscription.form.login.exists"));
		}

		// --- mot de passe
		if (GenericValidator.isBlankOrNull(password)) {
			errors.add("password", new ActionMessage("inscription.form.password.required"));
		} else if (!GenericValidator.minLength(password, PASSWORD_MIN_LENGTH)) {
			errors.add("password", new ActionMessage("inscription.form.password.minLength", PASSWORD_MIN_LENGTH));
		} else if (!GenericValidator.maxLength(password, PASSWORD_MAX_LENGTH)) {
			errors.add("password", new ActionMessage("inscription.form.password.maxLength", PASSWORD_MAX_LENGTH));
		}

		// --- email
		if (GenericValidator.isBlankOrNull(email)) {
			errors.add("email", new ActionMessage("inscription.form.email.required"));
		} else if (!GenericValidator.isEmail(email)) {
			errors.add("email", new ActionMessage("inscription.form.email.format"));
		} else if (emailExists()) {
			errors.add("email", new ActionMessage("inscription.form.email.exists"));
		}

		// --- genre
		if (GenericValidator.isBlankOrNull(genre)) {
			errors.add("genre", new ActionMessage("inscription.form.genre.required"));
		}

		return errors;
	}

	// ========================================================================
	// METHODES 'PROTECTED'
	// ========================================================================

	protected boolean loginExists() {

		return userService.isLoginExists(login);
	}

	protected boolean emailExists() {

		return userService.isEmailExists(email);
	}

	// ========================================================================
	// GETTERS & SETTERS
	// ========================================================================

	/**
	 * @return the login
	 */
	public String getLogin() {
		return this.login;
	}

	/**
	 * @param pLogin
	 *            the login to set
	 */
	public void setLogin(String pLogin) {
		this.login = pLogin;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return this.password;
	}

	/**
	 * @param pPassword
	 *            the password to set
	 */
	public void setPassword(String pPassword) {
		this.password = pPassword;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return this.email;
	}

	/**
	 * @param pEmail
	 *            the email to set
	 */
	public void setEmail(String pEmail) {
		this.email = pEmail;
	}

	/**
	 * @return the genre
	 */
	public String getGenre() {
		return this.genre;
	}

	/**
	 * @param pGenre
	 *            the genre to set
	 */
	public void setGenre(String pGenre) {
		this.genre = pGenre;
	}
}
